package com.ltmonitor.jt808.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.app.GlobalConfig;
import com.ltmonitor.jt808.protocol.T808Message;
import com.ltmonitor.jt808.service.IAckService;
import com.ltmonitor.jt808.service.IGpsDataService;
import com.ltmonitor.jt808.service.IMessageProcessService;
import com.ltmonitor.jt808.service.IMultiPacketMessageService;
import com.ltmonitor.service.IMediaItemService;
/**
 * 消息分发类
 * @author admin
 *
 */
@Service("messageProcessService")
public class MessageProcessService implements IMessageProcessService {

	private static Logger logger = Logger.getLogger(MessageProcessService.class);
	@Autowired
	private IGpsDataService gpsDataService;

	@Autowired
	private IAckService ackService;
	

	@Autowired
	private IMultiPacketMessageService multiPacketMessageService;

	
	/**
	 * 开始处理收到的完整的808数据包
	 */
	public void processMsg(T808Message msgFromTerminal) {
		//分发到界面，将808消息日志显示在界面窗口的日志监控栏中
		GlobalConfig.putMsg(msgFromTerminal);
		
		int msgType = msgFromTerminal.getMessageType();
		if (msgType == 0)
			return;
		String simId = msgFromTerminal.getSimNo();
		try {
			// /如果有分包，如果不是0x0801的拍照上传包，则需要先进行分包聚合成完整的包后，才能交给gpsDataService处理
			if (msgFromTerminal.getHeader().getIsPackage() && msgType != 0x0801) {
				multiPacketMessageService.processMultiPacketMsg(msgFromTerminal);
			}else
			{			
			    // gps数据处理服务
			    gpsDataService.ProcessMessage(msgFromTerminal);
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
		}
		//应答服务
		this.ackService.beginAck(msgFromTerminal);
	}
	


}
